package com.dbt.admin.controller;

import com.dbt.admin.domain.entity.SysUser;
import com.dbt.admin.result.R;
import com.dbt.admin.service.MybatisTestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author fangkun
 * @create 2022-08-15-10:25
 */
@Slf4j
@RestController
public class MybatisTestController {

    @Autowired
    MybatisTestService mybatisTestService;

    @GetMapping("/selectById")
    public R selectById(@RequestParam int id){
        SysUser sysUser = mybatisTestService.selectById(id);
        return R.ok().data("user",sysUser);
    }

    @PostMapping("/insertUser")
    public R insertUser(@RequestBody SysUser user){

        int i = mybatisTestService.insertUser(user);

        if (i <= 0){
            return R.error().message("INSERT ERROR");
        }
        return R.ok().message("okok");
    }

    @PostMapping("/updateById/{id}")
    public R updateById(@PathVariable Long id){

        int i = mybatisTestService.updateById(id);

        if (i <= 0){
            return R.error().message("UPDATE ERROR");
        }

        return R.ok().message("UPDATE SUCCESS");
    }

    @PostMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Long id){

        int i = mybatisTestService.deleteById(id);

        if (i <= 0){
            return R.error().message("DELETE ERROR");
        }

        return R.ok().message("DELETE SUCCESS");
    }

    @GetMapping("/selectUser")
    public R selectUser(@RequestBody SysUser user){

        List<SysUser> userList = mybatisTestService.selectUser(user);

        return R.ok().data("userList",userList);
    }

}














